package project2.ui.manage;

import project2.config.Config;
import project2.entity.Score;
import project2.ui.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class Top10ScorePanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public GameButton okButton;
    private final int SCORE_COUNT = 10;
    private final GameButton[] scoreButtons = new GameButton[SCORE_COUNT];

    public Top10ScorePanel(MainFrame mainFrame) {
        JLabel top10ScoreLabel = new JLabel("<html><font size='5'>Top 10 Scores</font></html>");// 标题

        JPanel labelPanel = new JPanel();
        labelPanel.setOpaque(false);// 设置为透明
        labelPanel.add(top10ScoreLabel); // 默认流式居中布局

        this.okButton = new GameButton("OK");// 确认按钮
        this.okButton.addActionListener(mainFrame.popupMenuPanel.menuChoose);

        JPanel scorePanel = new JPanel();
        scorePanel.setLayout(new GridLayout(12, 1, 0, 5));// 网格布局
        scorePanel.setOpaque(false);
        scorePanel.add(labelPanel);// 第一行放标题
        for (int i = 0; i < SCORE_COUNT; i++) {// 后面放10行按钮
            this.scoreButtons[i] = new GameButton();
            scorePanel.add(this.scoreButtons[i]);
        }
        scorePanel.add(okButton);// 最后一行放确认按钮
        // 设置宽高
        Dimension d = new Dimension(Config.POP_UP_SCORE_PANEL_WIDTH, Config.POP_UP_SCORE_PANEL_HEIGHT);
        scorePanel.setSize(d);
        scorePanel.setPreferredSize(d);

        this.setOpaque(false);
        this.add(scorePanel);// 默认流式居中布局
    }

    /**
     * 10行按钮上加载分数
     */
    public void loadScore(List<Score> sortedScoreList) {
        int scoreSize = sortedScoreList.size();
        for (int i = 0; i < scoreSize; i++) {
            Score score = sortedScoreList.get(i);
            this.scoreButtons[i].setText(score.getScore() + "");
        }
    }
}
